package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SportDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Sport;
import com.itheima.service.SportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service(interfaceClass = SportService.class)
@Transactional
public class SportServiceImpl implements SportService {

    @Autowired
    private SportDao sportDao;

    //新增运动项
    public void add(Sport sport) {
        Sport findSport = sportDao.findSportBySportName(sport.getName());
        if (findSport == null) {
            //没有则添加
            sportDao.add(sport);
        } else {
            //有则更新
            sportDao.update(sport);
        }
    }

    //分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Sport> page = sportDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //根据id删除类
    public void delete(Integer id) {
        sportDao.delete(id);
    }
}
